<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-row :gutter="10" class="mb8">
        <el-col :span="4">
          <el-input
            v-model="queryParams.pmrQuery"
            placeholder="请输入退料单编号/名称"
            clearable
            @keyup.enter.native="handleQuery"
            style="width: 100%"
          />
        </el-col>
        <el-col :span="4">
          <el-input
            v-model="queryParams.workOrderQuery"
            placeholder="请输入工单编号/名称"
            clearable
            @keyup.enter.native="handleQuery"
            style="width: 100%"
          />
        </el-col>
        <el-col :span="4">
          <el-date-picker clearable
                          v-model="queryParams.pmrReturnDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择退料日期"
                          style="width: 100%"
          >
          </el-date-picker>
        </el-col>
        <el-col :span="4">
          <el-input
            v-model="queryParams.workTaskQuery"
            placeholder="请输入任务编号"
            clearable
            @keyup.enter.native="handleQuery"
            style="width: 100%"
          />
        </el-col>
        <el-col :span="4">
          <el-input
            v-model="queryParams.pmrReturnPeo"
            placeholder="请输入退料人"
            clearable
            @keyup.enter.native="handleQuery"
            style="width: 100%"
          />
        </el-col>
      </el-row>
      <el-row :gutter="10" class="mb8">
        <el-col :span="4">
          <el-select
            v-model="queryParams.prouteCode"
            placeholder="请选择工艺路线编号"
            clearable
            @visible-change="handleRouteVisibleChange"
            style="width: 100%">
            <el-option v-for="item in routeList" :key="item.prouteCode" :label="item.prouteName" :value="item.prouteCode" />
          </el-select>
        </el-col>
        <el-col :span="4">
          <el-select
            v-model="queryParams.pmrWorkProcedure"
            placeholder="请选择工序编号"
            clearable
            @visible-change="handleProcedureVisibleChange"
            style="width: 100%">
            <el-option v-for="item in procedureList" :key="item.procedureCode" :label="item.procedureName" :value="item.procedureCode" />
          </el-select>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wm:inbound:material_return:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wm:inbound:material_return:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wm:inbound:material_return:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wm:inbound:material_return:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="material_returnList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="退料单编码" align="center" prop="pmrCode" fixed width="180">
        <template slot-scope="scope">
          <span @click="handleDetail(scope.row)" style="color: #00afff">
            {{scope.row.pmrCode}}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="退料单名称" align="center" prop="pmrName" fixed width="120">
        <template slot-scope="scope">
          <a href="#" @click="handleDetail(scope.row)">{{ scope.row.pmrName }}</a>
        </template>
      </el-table-column>
      <el-table-column label="退料日期" align="center" prop="pmrReturnDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.pmrReturnDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退料人" align="center" prop="pmrReturnPeoName" width="120" />
      <el-table-column label="工单编号" align="center" prop="pmrWorkOrder" width="180" />
      <el-table-column label="工单名称" align="center" prop="pmrWorkOrderName" width="120" />
      <el-table-column label="任务编号" align="center" prop="pmrWorkTask" width="150" />
      <el-table-column label="任务名称" align="center" prop="pmrWorkTaskName" width="120" />
      <el-table-column label="工艺路线" align="center" prop="prouteName" width="120" />
      <el-table-column label="工序名称" align="center" prop="procedureName" width="120" />
      <el-table-column label="物料信息" align="center" prop="materialCount" width="120" >
        <template slot-scope="scope">
          <span>{{ scope.row.materialCount }}种</span>
        </template>
      </el-table-column>
      <el-table-column label="领料总数" align="center" prop="requestNum" width="120" />
      <el-table-column label="备注" align="center" prop="pmrDesc" width="120" />
      <el-table-column label="状态" align="center" prop="pmrStatus" width="120" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="180">
        <template slot-scope="scope">
          <div v-if="scope.row.pmrStatus === '0'">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-box"
              @click="handleReturn(scope.row)"
              v-has-permi="['wm:inbound:material_return:store']"
            >退料</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['wm:inbound:material_return:edit']"
            >编辑</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['wm:inbound:material_return:remove']"
            >删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  listMaterial_return,
  delMaterial_return,
  getRouteCode, getProcedureCode, storeMaterial_return
} from '@/api/yyj/wm/inbound/material_return'
import { parseTime } from '@/utils/ruoyi'

export default {
  name: "Material_return",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产退料表格数据
      material_returnList: [],
      // 工艺路线数据
      routeList: [],
      // 工序数据
      procedureList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pmrReturnDate: null,
        pmrReturnPeo: null,
        pmrWorkProcedure: null,
        pmrQuery: null,
        workOrderQuery: null,
        workTaskQuery: null,
        prouteCode: null,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    parseTime,
    /** 查询生产退料列表 */
    getList() {
      this.loading = true;
      listMaterial_return(this.queryParams).then(response => {
        this.material_returnList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 查询参数重置
    resetQueryParams() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        pmrReturnDate: null,
        pmrReturnPeo: null,
        pmrWorkProcedure: null,
        pmrQuery: null,
        workOrderQuery: null,
        workTaskQuery: null,
        prouteCode: null,
      };
      this.resetForm("queryForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetQueryParams();
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.pmrCode)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({
        path: '/warehouse/inbound/material_return/add'
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const pmrCode = row.pmrCode || this.ids;
      this.$router.push({
        path: '/warehouse/inbound/material_return/edit',
        query: {
          pmrCode: pmrCode
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const pmrCodes = row.pmrCode || this.ids;
      this.$modal.confirm('是否确认删除生产退料编号为"' + pmrCodes + '"的数据项？').then(function() {
        return delMaterial_return(pmrCodes);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wm/inbound/material_return/export', {
        ...this.queryParams
      }, `material_return_${new Date().getTime()}.xlsx`)
    },
    // 工艺路线下拉框事件
    handleRouteVisibleChange(isVisible) {
      if (isVisible) {
        getRouteCode().then(response => {
          this.routeList = response.data;
        })
      }
    },
    // 工序下拉框事件
    handleProcedureVisibleChange(isVisible) {
      if (isVisible) {
        getProcedureCode().then(response => {
          this.procedureList = response.data;
        })
      }
    },
    // 退料事件
    handleReturn(row) {
      const pmrCode = row.pmrCode;
      const pmrStatus = 1;

      this.$modal.confirm('是否确认退料入库编号为\"' + pmrCode + '\"的数据项？').then(function() {
        return storeMaterial_return({
          pmrCode,
          pmrStatus
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("入库成功");
        }).catch(() => {})
      })
    },
    // 查看退料详情
    handleDetail(row) {
      const pmrCode = row.pmrCode;
      this.$router.push({
        path: '/warehouse/inbound/material_return/detail',
        query: {
          pmrCode: pmrCode
        }
      })
    }
  }
};
</script>
